package com.yl.util;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

@Slf4j
public class CmdUtil {

    /**
     * 执行cmd命令
     */
    public static void exec(String cmd) {
        exec(cmd, null);
    }

    /**
     * 执行cmd命令
     *
     * @param cmd 执行命令
     * @param dir 执行目录
     */
    public static void exec(String cmd, String dir) {
        Process proc;
        try {
            proc = null != dir ? Runtime.getRuntime().exec(cmd, null, new File(dir)) : Runtime.getRuntime().exec(cmd, null);
            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                log.info(line);
            }
            in.close();
            proc.waitFor();
        } catch (Exception e) {
            log.error("err occur in execPy, {}, {}", cmd, dir);
            e.printStackTrace();
        }
    }
}
